<?php
defined('BY_JYA') or exit('error');

cfc('core')->checklogin();
if($_RQ['code']){
	if(strlen($_RQ['code'])!=10 || preg_match('/\W/',$_RQ['code'])){
		exi('访问错误！','error');
	}
	$_SESSION['jy_move_agentcar']['card_code']=$_RQ['code'];
}
if(!$_SESSION['jy_move_agentcar']['card_code']){
	exi('访问错误！','error');
}
$card=pdo_get('jy_move_agentcar_cards',array('code'=>$_SESSION['jy_move_agentcar']['card_code']));
if(!$_RQ['op']){
	if(!$card['mid']){
		$_RQ['op']='active';
	}else{
		$_RQ['op']='move';
	}
}

if($_RQ['op']=='active'){
	//激活
	if($_RQ['postdata']){
		$post=get_postdata();
		if($setting['verify'] && (!$post['code'] || !check_sms_code($post['code']))){
			exi('验证码输入错误！','error');
		}
		$update=array_fields(array('plate','engine_number','frame_number','brand','brandimg'),$post);
		if($update['plate']){
			$update['plate']=ijson_encode($update['plate']);
		}
		if(!$update['plate']){
			exi('请先设置车牌号~','error');
		}
		if(!$update['engine_number']){
			exi('请先填写发动机号','error');
		}
		if(!$update['frame_number']){
			exi('请先设置车架号~','error');
		}
		$update['type']=2;
		$update['mobile']=$post['mobile'];
		$update['activetime']=TIMESTAMP;
		model('core/member')->member_update($_SESSION['uid'],array('mobile'=>$post['mobile']));
		pdo_update('jy_move_agentcar_member',$update,array('mid'=>$_SESSION['uid']));
		$update=array(
			'mid'=>$_SESSION['uid'],
			'status'=>1,
			'activetime'=>TIMESTAMP,
		);
		pdo_update('jy_move_agentcar_cards',$update,array('id'=>$card['id']));
		exi('激活成功！','','index/');
	}
	$item=pdo_get('jy_move_agentcar_member',array('mid'=>$_SESSION['uid']),array('mobile','plate','engine_number','frame_number','brand','brandimg'));
	$item['card_number']=$this->card_number($card['number']);
	$item['card_id']=$card['id'];
	if($item['plate']){
		$item['plate']=json_decode($item['plate'],true);
	}else{
		$item['plate']=array('p'=>'京','c'=>'A');
	}
	$return['item']=$item;
	$return['brands']=$this->car_brand();
	$return['active_brand']=$return['brands'][0]['childs'][0];
	$setting=$this->plugin_setting('card');
	$return['check_mobile']=$setting['check_mobile'];
	/*$return['htmls']=array(
		'title'=>'激活挪车卡',
		'list'=>array(
			array('type'=>'string','name'=>'card_number','title'=>'卡号'),
			array('type'=>'string','name'=>'mobile','title'=>'手机号'),
			array('type'=>'mobile_code','name'=>'code','title'=>'验证码','mobile_name'=>'mobile','send_op'=>'send','help'=>''),
			array('type'=>'submit','post'=>true,'url'=>'active','data'=>'item','title'=>'提交'),
		),
	);*/
	$return['htmls']=$this->template_url('member/mycar');
	exi($return);
}
if($_RQ['op']=='move'){
	//挪车
	if($_SESSION['uid']==$card['mid']){
		//挪车卡是自己的，直接跳转会员中心
		exi('','','index/');
	}
	if($card['type']==2){
	//添加扫描记录
		$this->scan_log($card['number']);
	}
	
	$car_user=pdo_get('jy_move_agentcar_member',array('mid'=>$card['mid']),array('plate','message'));
	$car_user['plate']=json_decode($car_user['plate'],true);
	$car_user['plate']="{$car_user['plate']['p']}{$car_user['plate']['c']}{$car_user['plate']['number']}";
	$car_user['message']=$car_user['message']?:'事情紧急停车匆忙，给您带来不便，尽请谅解！点击通知，我会火速赶来！';
	$return['car_user']=$car_user;
	$member=pdo_get('jy_move_agentcar_member',array('mid'=>$_SESSION['uid']),array('mobile'));
	$return['setting']=$this->plugin_setting('notice');
	$return['item']=$member;
	exi($return);
}
if($_RQ['op']=='notice'){
	//通知车主
	//检查挪车次数是否超出、挪车间隔是否合法
	$setting=$this->plugin_setting('notice');
	$setting['type']=str_array($setting['type']);
	if($setting['max_num']){
		//日挪车次数限制
		$num=pdo_count('jy_move_agentcar_move_log',array('mid'=>$_SESSION['uid'],'createtime >'=>strtotime(date('Ymd'))));
		if($num>=$setting['max_num']){
			exi('您今天挪车次数太多了！明天再挪吧！','error');
		}
	}
	if($setting['glue']){
		//挪车频率限制
		$last=pdo_fetchcolumn('select `createtime` from '.tablename('jy_move_agentcar_move_log').' where mid='.$_SESSION['uid'].' and card_id='.$card['id'].' order by id desc limit 1');
		if(TIMESTAMP-$last<$setting['glue']*60){
			exi('请勿频繁挪车！','error');
		}
	}
	$post=get_postdata();
	if($setting['verify'] && (!$post['verifycode'] || $post['verifycode']!=$_SESSION['verify_code'])){
		exi('验证码输入错误！','error');
	}
	model('core/member')->member_update($_SESSION['uid'],array('mobile'=>$post['mobile']));
	$card_user=pdo_get('jy_move_agentcar_member',array('mid'=>$card['mid']),array('plate','mobile'));
	$plate=json_decode($card_user['plate'],true);
	$plate="{$plate['p']}{$plate['c']}{$plate['number']}";
	$notice_type=intval($post['notice_type']);
	$move_log=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'card_id'=>$card['id'],
		'notice_type'=>$notice_type,
		'address'=>$post['address'],
		'plate'=>$plate,
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('jy_move_agentcar_move_log',$move_log);
	//以下开始通知车主
	if($notice_type==1){
		//消息通知
		if(in_array('wx',$setting['type'])){
			$template_id=cfc('account')->get_tpl_template_id('OPENTM206445801','挪车通知模版');
			$message="尊敬的{$plate}车主，您收到一条挪车请求！请快速前往挪车！";
			$postdata=array(
				'first'=>array('value'=>'挪车提醒','color'=>'#73a68d'),
				'keyword1'=>array('value'=>$_SESSION['member']['nickname'],'color'=>'#73a68d'),
				'keyword2'=>array('value'=>$message,'color'=>'#73a68d'),
				'remark'=>array('value'=>'挪车提醒','color'=>'#73a68d'),
			);
			cfc('account')->sendTplNotice($card['mid'],$template_id,$postdata);
			
			//cfc('account')->send_text($message,$card['mid']);
		}
		if($card_user['mobile']){
			$template_setting=$this->plugin_setting('template');
			$message_data=array(
				'plate'=>$plate,
			);
			if(in_array('sms',$setting['type'])){
				//短信通知
				if(!$template_setting['sms_move']){
					exi('短信发送失败，请联系管理员设置好短信接口参数！','error');
				}
				$this->plugin('jy_alisms')->sendsms($card_user['mobile'],$message_data,$template_setting['sms_move']);
			}
			if(in_array('voice',$setting['type'])){
				//语音通知
				if(!$template_setting['voice_move']){
					exi('短信发送失败，请联系管理员设置好语音短信接口参数！','error');
				}
				$this->plugin('jy_alisms')->single($card_user['mobile'],$message_data,$template_setting['voice_move']);
			}
		}
		exi('已成功通知车主！','','index/');
	}else{
		//电话通知
		if(!$post['mobile']){
			exi('请先输入手机号！','error');
		}
		$car_user_mobile=pdo_getcolumn('jy_move_agentcar_member',array('mid'=>$card['mid']),'mobile');
		if(!$car_user_mobile){
			exi('车主未设置手机号码，无法接通！','error');
		}
		$mid_mobile=$this->plugin('jy_alisms')->bind($post['mobile'],$car_user_mobile);
		if(is_error($mid_mobile)){
			exi($mid_mobile['message'],'error');
		}
		$res=array('status'=>'success','mobile'=>$mid_mobile);
		exi($res);
	}
}
